package com.xdcplus.web.service.wip.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageInfo;
import com.xdcplus.core.utils.BeanUtils;
import com.xdcplus.exceptions.XdcWebException;
import com.xdcplus.mom.constants.NumberConstant;
import com.xdcplus.mom.enums.ResponseEnum;
import com.xdcplus.mp.service.impl.BaseServiceImpl;
import com.xdcplus.mp.utils.PageableUtils;
import com.xdcplus.mp.utils.PropertyUtils;
import com.xdcplus.pager.vo.PageVO;
import com.xdcplus.web.common.pojo.dto.wip.WipMarkingReminderDTO;
import com.xdcplus.web.common.pojo.dto.wip.WipMarkingReminderFilterDTO;
import com.xdcplus.web.common.pojo.entity.wip.WipMarkingReminder;
import com.xdcplus.web.common.pojo.query.wip.WipMarkingReminderQuery;
import com.xdcplus.web.common.pojo.vo.wip.WipMarkingReminderVO;
import com.xdcplus.web.mapper.wip.WipMarkingReminderMapper;
import com.xdcplus.web.service.wip.WipMarkingReminderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * 打标提醒人员表(推送钉钉)(WipMarkingReminder)表服务实现类
 *
 * @author Devin.sang
 * @since 2023-05-15 22:05:44
 */
@Slf4j
@Service("wipMarkingReminderService")
public class WipMarkingReminderServiceImpl extends BaseServiceImpl<WipMarkingReminderMapper, WipMarkingReminder, WipMarkingReminder, WipMarkingReminderVO> implements WipMarkingReminderService {

    @Autowired
    protected WipMarkingReminderMapper wipMarkingReminderMapper;

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean saveWipMarkingReminder(WipMarkingReminderDTO wipMarkingReminderDTO) {

        WipMarkingReminder wipMarkingReminder = new WipMarkingReminder();
        BeanUtil.copyProperties(wipMarkingReminderDTO, wipMarkingReminder);

        return this.save(wipMarkingReminder);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean updateWipMarkingReminder(WipMarkingReminderDTO wipMarkingReminderDTO) {

        WipMarkingReminder wipMarkingReminder = this.getById(wipMarkingReminderDTO.getId());
        if (ObjectUtil.isNull(wipMarkingReminder)) {
            log.error("updateWipMarkingReminder() The WipMarkingReminder does not exist or has been deleted");
            throw new XdcWebException(ResponseEnum.ERROR);
        }

        BeanUtil.copyProperties(wipMarkingReminderDTO, wipMarkingReminder);

        return this.updateById(wipMarkingReminder);
    }

    @Override
    public Boolean saveOrUpdateBatch(List<WipMarkingReminder> wipMarkingReminderList) {

        wipMarkingReminderList.forEach(wipMarkingReminder -> {
            WipMarkingReminder wipMarkingReminderParam = new WipMarkingReminder();
            wipMarkingReminderParam.setId(wipMarkingReminder.getId());
            if (ObjectUtil.isNotNull(wipMarkingReminder.getId())) {
                wipMarkingReminder.setId(wipMarkingReminder.getId());
                LambdaUpdateWrapper<WipMarkingReminder> lambdaUpdate = Wrappers.lambdaUpdate();
                lambdaUpdate.eq(WipMarkingReminder::getId, wipMarkingReminder.getId());
                update(wipMarkingReminder, lambdaUpdate);
            } else {
                save(wipMarkingReminder);
            }
        });
        return true;
    }

    @Override
    public Boolean saveOrUpdateBatchByDTOList(List<WipMarkingReminderDTO> wipMarkingReminderDTOList) {

        List<WipMarkingReminder> wipMarkingReminderList = BeanUtils.copyProperties(wipMarkingReminderDTOList, WipMarkingReminder.class);
        return saveOrUpdateBatch(wipMarkingReminderList);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean deleteWipMarkingReminderLogical(Long id) {

        Assert.notNull(id, ResponseEnum.THE_ID_CANNOT_BE_EMPTY.getMessage());

        WipMarkingReminder wipMarkingReminder = this.getById(id);

        if (ObjectUtil.isNull(wipMarkingReminder)) {
            log.error("deleteWipMarkingReminder() The WipMarkingReminder does not exist or has been deleted");
            throw new XdcWebException(ResponseEnum.ERROR);
        }
        return this.removeById(id);
    }
    
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean deleteWipMarkingReminderByIds(String ids) {
        Assert.notNull(ids, ResponseEnum.THE_ID_CANNOT_BE_EMPTY.getMessage());
		List<String> idList = Arrays.asList(ids.split(","));
		if(CollectionUtil.isEmpty(idList)){
           throw new XdcWebException(ResponseEnum.ERROR);
       }
		idList.forEach(id->{
			WipMarkingReminder wipMarkingReminder = this.getById(id);
			if (ObjectUtil.isNull(wipMarkingReminder)) {
				log.error("deleteWipMarkingReminder() The WipMarkingReminder does not exist or has been deleted");
				throw new XdcWebException(ResponseEnum.ERROR);
			}
		});
		
        return this.removeByIds(idList);
    }

    private List<WipMarkingReminder> queryWipMarkingReminderList(WipMarkingReminderFilterDTO wipMarkingReminderFilterDTO) {
        WipMarkingReminderQuery wipMarkingReminderQuery = BeanUtil.copyProperties(wipMarkingReminderFilterDTO, WipMarkingReminderQuery.class);

        return wipMarkingReminderMapper.queryWipMarkingReminder(wipMarkingReminderQuery);
    }

    @Override
    public List<WipMarkingReminderVO> queryWipMarkingReminderVOList(WipMarkingReminderFilterDTO wipMarkingReminderFilterDTO) {
        return this.objectConversion(queryWipMarkingReminderList(wipMarkingReminderFilterDTO));
    }

    @Override
    public PageVO<WipMarkingReminderVO> queryWipMarkingReminder(WipMarkingReminderFilterDTO wipMarkingReminderFilterDTO) {
        PageVO<WipMarkingReminderVO> pageVO = new PageVO<>();

        if (wipMarkingReminderFilterDTO.getCurrentPage() > NumberConstant.ZERO) {
            PageableUtils.basicPage(wipMarkingReminderFilterDTO.getCurrentPage(), wipMarkingReminderFilterDTO.getPageSize(),
                    wipMarkingReminderFilterDTO.getOrderType(), wipMarkingReminderFilterDTO.getOrderField());
        }

        List<WipMarkingReminder> wipMarkingReminderList = queryWipMarkingReminderList(wipMarkingReminderFilterDTO);

        PageInfo<WipMarkingReminder> pageInfo = new PageInfo<>(wipMarkingReminderList);
        PropertyUtils.copyProperties(pageInfo, pageVO, this.objectConversion(wipMarkingReminderList));

        return pageVO;
    }

    @Override
    public WipMarkingReminderVO queryWipMarkingReminderById(Long id) {

        Assert.notNull(id, ResponseEnum.THE_ID_CANNOT_BE_EMPTY.getMessage());

        return this.objectConversion(this.getById(id));
    }
}
